其他
4000字,详解Python操作MySQL数据库!
1. 通用步骤
import pymysql
# 2. 链接MySQL服务器
db = pymysql.connect(host='localhost' , user='root' , password='******' , port=3306 ,db='spiders' , charset='utf8')
# 3. 创建一个cursor游标对象
cursor = db.cursor()
# 4. 在这一步写你的sql语句
sql = 'select version()'
# 5. 执行sql语句
cursor.execute(sql)
# 6. 断开连接
db.close()
2. 需要特别说明的知识点
Ⅰ 各参数说明
参数 1 :mysql 服务器所在的主机 IP 参数 2 :用户名; 参数 3 :密码; 参数 4 :连接的 mysql 主机的端口,默认是 3306; 参数 5 :连接的数据库名; 参数 6 :通信采用的编码方式,默认是'gb2312',要求与数据库创建时指定的编码一致,否则中文会乱码;
Ⅱ cursor游标对象
3. Python增删改查操作
① 创建一个数据表
db = pymysql.connect(host='192.168.3.47' , user='root',password='******' , port=3306 , db='spiders' , charset='utf8')
cursor = db.cursor()
# 检查表是否存在,如果存在删除
cursor.execute('drop table if exists students')
# 创建表
sql = 'create table students(id int auto_increment primary key not null,name varchar(10) not null,age int not null)'
cursor.execute(sql)
db.close()
② 插入数据
db = pymysql.connect(host='192.168.3.47' , user='root',password='******' , port=3306 , db='spiders' , charset='utf8')
cursor = db.cursor()
# 插入数据
sql = 'insert into students(name,age) values(%s,%s)'
try:
cursor.execute(sql,('孙悟空',100000))
db.commit()
except:print("插入失败")
db.rollback()
db.close()
③ 更新数据
db = pymysql.connect(host='192.168.3.47' , user='root',password='******' , port=3306 , db='spiders' , charset='utf8')
cursor = db.cursor()
# 更新数据
sql = 'update students set age =%s where name=%s'
try:
cursor.execute(sql,(30,"郭卫华"))
db.commit()
except:
print("插入失败")
db.rollback()
db.close()
③ 删除操作
db = pymysql.connect(host='192.168.3.47' , user='root',password='******' , port=3306 , db='spiders' , charset='utf8')
cursor = db.cursor()
# 删除数据
sql = 'delete from students where age=100000'
try:
cursor.execute(sql)
db.commit()
except:
print("插入失败")
db.rollback()
db.close()
④ 查询操作
fetchone()功能:获取下一个查询结果集,结果集是一个对象。 fetchall()功能:接收全部返回的行。
db = pymysql.connect(host='192.168.3.47' , user='root',password='******' , port=3306 , db='spiders' , charset='utf8')
cursor = db.cursor()
# 查询数据
sql = 'select * from students where age>60'
try:
cursor.execute(sql)
reslist = cursor.fetchall()
for row in reslist:
print(“%d--%d” %(row[0],row[1],… row[n]))
except:
print("插入失败")
db.rollback()
db.close()
4. 封装一个类
import pymysql
class StudentsSql():
def __init__(self,host,user,port,dbname,charset):
self.host = host
self.user = user
self.port = port
self.dbname = dbname
self.charset = charset
def connet(sef):
self.db = pymysql.connect(self.host, self.user, self.port,
self.dbname, self.charset)
def close(self):
self.cursor.close()
self.db.close()
def get_one(self,sql):
res = None
try:
self.connect()
self.cursor.execute(sql)
res = self.cursor.fetchone()
self.close()
except:
print(“查询失败”)
return res
def get_all(self,sql):
res = None
try:
self.connect()
self.cursor.execute(sql)
res = self.cursor.fetchall()
self.close()
except:
print(“查询失败”)
return res
def inset(self,sql):
return self.__edit(sql)
def update(self,sql):
return self.__edit(sql)
def delete(self,sql):
return self.__edit(sql)
def __edit(self,sql):
count = 0
try:
self.connect()
count = self.cursor.execute(sql)
self.db.commit()
self.close()
except:
print(“事务提交失败”)
self.db.rollback()
return count
s = StudentsSql(“host='192.168.3.47' , user='root', password='******' , port=3306 , db='spiders' , charset='utf8'”)
res = s.get_all('select * from students where age>60')
for row in res:
print(“%d--%d” %(row[0],row[1],… row[n]))
后台回复:手册
获取mysql原创手册